package com.sh.suhe.utildemos.views.activity

import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import com.sh.suhe.utildemos.R
import com.sh.suhe.utildemos.utils.OSUtils
import kotlinx.android.synthetic.main.activity_sys_param.*
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.util.HashMap



class SysParamActivity : AppCompatActivity() {
    private var TAG = "SysParamActivity"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sys_param)
        val constants = HashMap<String, Any>()
//        constants.put("serialNumber", Build.getSerial())
        constants.put("systemName", "Android")
        constants.put("systemVersion", Build.VERSION.RELEASE)
        constants.put("model", Build.MODEL)
        constants.put("brand", Build.BRAND)
        constants.put("deviceId", Build.BOARD)
        constants.put("apiLevel", Build.VERSION.SDK_INT)
//        constants.put("deviceLocale", this.getCurrentLanguage())
//        constants.put("deviceCountry", this.getCurrentCountry())
        constants.put("uniqueId", Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID))
        constants.put("systemManufacturer", Build.MANUFACTURER)
        constants.put("bundleId", packageName)
        constants["ID"] =  Build.ID
        val romType = OSUtils.getRomType()
        constants["romType"] =  romType
        constants["miuiCode"] =  getSystemProperty("ro.miui.ui.version.code")
        constants["miuiName"] =  getSystemProperty("ro.miui.ui.version.name")
        constants["emuiCode"] =  getSystemProperty("ro.build.version.emui")
        var tip = ""
        var set = constants.keys
        for (  item in set){
            tip += item +":"+ constants[item] +" \n "
        }
        sys_tip.text = tip
    }

    fun getEMUI(): String {
        var classType: Class<*>? = null
        var buildVersion: String = ""
        try {
            classType = Class.forName("android.os.SystemProperties")
            val getMethod = classType!!.getDeclaredMethod("get", *arrayOf<Class<*>>(String::class.java))
            buildVersion = getMethod.invoke(classType, arrayOf<Any>("ro.build.version.emui")) as String
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return buildVersion
    }
    fun     getSystemProperty(  propName:String):String{
        var line = ""
        var  input: BufferedReader? = null
        try
        {
            var p = Runtime.getRuntime().exec("getprop " + propName);
            input =   BufferedReader(  InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine()
            input.close()
        }
        catch ( ex: IOException) {
            Log.e(TAG, "Unable to read sysprop " + propName, ex);
            return ""
        }
        finally {
            if(input != null) {
                try {
                    input.close()
                }
                catch ( e:IOException) {
                    Log.e(TAG, "Exception while closing InputStream", e)
                }
            }
        }
        return line
    }

}
